package com.zhidisoft.system.dao;

import com.zhidisoft.system.entity.User;
import com.zhidisoft.util.DBUtil;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type User dao 2.
 */
public class UserDao2 {


    /**
     * 根据username获取用户对象
     *
     * @param username 指定 username
     * @return 用户对象
     */
    public User getUser(String username) {
        String sql = "select * from tb_user where username = ? ";
        List<Map<String, String>> list = DBUtil.query(sql, username);
        Map<String, String> map = new HashMap();
        if (list != null && list.size() == 1) {
            map = list.get(0);
        }
        User user = new User();
        try {
            //map中的数据映射到JavaBean中的get和set方法中（封装数据到JavaBean中）。之后取值就直接从JavaBean中的get和set方法中取值就可以了
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return user;
    }

    public  User getUserById(Integer id){
        String sql = "select * from tb_user where id = ?";
        Map<String, String> result = DBUtil.queryRow(sql, id);
        User user = new User();
        if (!result.isEmpty() && result.size()>0){
            try {
                BeanUtils.populate(user,result);
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    public void update(){

    }

}
